<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item :label="$locale('geWudevice.Equipment name')" prop="deviceName">
            <el-input
              v-model="queryParams.deviceName"
              :placeholder="$locale('geWudevice.Please enter the device name')"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item :label="$locale('geWudevice.add.state')" prop="status">
            <el-select
              v-model="queryParams.status"
              :placeholder="$locale('geWudevice.add.state')"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.experimental_status"
                :key="dict.value"
                :label="dict.label"
                :value="+dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >{{ $locale('geWudevice.add.search for') }}</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >{{ $locale('geWudevice.reset') }}</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              >新增</el-button
            >
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="deviceList">
          <el-table-column type="index" width="50" :label="$locale('geWudevice.add.Serial Number')" />
          <el-table-column
            :label="$locale('geWudevice.Equipment number')"
            align="center"
            key="deviceNum"
            prop="deviceNum"
          />
          <el-table-column
            :label="$locale('geWudevice.Equipment name')"
            align="center"
            key="deviceName"
            prop="deviceName"
            width="160"
          >
          </el-table-column>
          <el-table-column :label="$locale('geWudevice.add.state')" align="center" key="status">
            <template slot-scope="scope">
              <span
                v-for="item in dict.type.experimental_status"
                :key="item.value"
                >{{ item.value == scope.row.status ? item.label : '' }}</span
              >
            </template>
          </el-table-column>
          <el-table-column
            :label="$locale('deviceList.add.Price (RMB/Yuan)')"
            align="center"
            key="priceRmb"
            prop="priceRmb"
          >
          </el-table-column>
          <el-table-column
            :label="$locale('deviceList.add.Price (USD)')"
            align="center"
            key="priceUsd"
            prop="priceUsd"
          >
          </el-table-column>
          <el-table-column
            :label="$locale('geWudevice.founder')"
            align="center"
            key="createBy"
            prop="createBy"
          />
          <el-table-column
            :label="$locale('geWudevice.Creation time')"
            align="center"
            prop="createTime"
            width="160"
          >
          </el-table-column>
          <el-table-column
            :label="$locale('geWudevice.add.describe')"
            align="center"
            key="description"
            prop="description"
          >
            <template slot-scope="scope">
              <span v-html="scope.row.description"></span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$locale('formMarkering.list.operate')"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >{{ $locale('classList.add.modify') }}</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >{{ $locale('formMarkering.list.delete') }}</el-button
              >
              <el-button size="mini" type="text" @click="handleBox(scope.row)"
                >{{ $locale('myDevice.determinedList.turnover box') }}</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加 -->
    <addDeviceList ref="addDeviceList" @getList="getList"></addDeviceList>
    <el-dialog :title="$locale('myDevice.determinedList.turnover box')" :visible.sync="open" width="500px">
      <el-table :data="listData" :max-height="400">
        <el-table-column type="index" width="50" :label="$locale('geWudevice.add.Serial Number')" />
        <el-table-column
          :label="$locale('myDevice.undetermined.Name of turnover box')"
          align="center"
          key="turnoverBoxName"
          prop="turnoverBoxName"
        />
        <el-table-column
          :label="$locale('myDevice.undetermined.Turnover box number')"
          align="center"
          key="turnoverBoxNum"
          prop="turnoverBoxNum"
        />
        <el-table-column
          :label="$locale('geWudevice.code')"
          align="center"
          key="deviceNum"
          prop="deviceNum"
        />
        <el-table-column
          :label="$locale('geWudevice.quantity')"
          align="center"
          key="deviceTotal"
          prop="deviceTotal"
        />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { deleteById, list } from '@/api/device/deviceList'
import { getTurnoverBoxListByDeviceId } from '@/api/device/turnoverBoxList'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import addDeviceList from './add/index.vue'
import { getNames } from '@/utils/index'

export default {
  name: 'experiment',
  dicts: ['experimental_status'],
  components: { Treeselect, addDeviceList },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      deviceList: [],
      listData: [],
      // 弹出层标题
      title: '',
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName:'',
        status:''
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true
      list(this.queryParams).then(({ data }) => {
        this.deviceList = data.records
        this.total = data.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.addDeviceList.handleAdd()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.addDeviceList.handleUpdate(row)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.id || this.ids
      this.$modal
        .confirm('是否确认删除名称为"' + getNames(this.deviceList, userIds, 'deviceName') + '"的数据项？')
        .then(function () {
          return deleteById({ id: userIds })
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 周转箱按钮操作 */
    handleBox(row) {
      this.open = true
      getTurnoverBoxListByDeviceId(row.id).then(({ data }) => {
        this.listData = data
        this.open = true
      })
    },
  },
}
</script>
